'use strict'

const log4js = require('koa-log4')
const logger = log4js.getLogger('app')
const URLConfig = require('../config/config')
const RequestUtil = require('../utils/requestUtil')
const createToken = require('../token/createToken')
const RedisStore = require('../config/redis')
exports.signupTest = async (ctx, next) => { 
	var content = ctx.request.body;
  var url = URLConfig.url.user.logintest;  
  logger.info("用户登录："+url+",request:"+JSON.stringify(content));
  var responseData = await RequestUtil.POST(ctx,url,content); // 这里调用要在 async 函数里
  logger.info("用户登录response："+JSON.stringify(responseData));
  // ctx.response.body = responseData;  
  let user = responseData;
  if(user.success){
    const userInfo = {
      id: user.obj.id,
      loginname: user.obj.loginname,
      name: user.obj.name,
      mobile:user.obj.mobile
    }
    ctx.session.userInfo = userInfo;
  }else{
    ctx.response.body = {
      code: 200,
      msg: 'false'
    }
  }
}
/**
 * 注册新用户
 * @param {Function} next          [description]
 * @yield {[type]}   [description]
 */
exports.signup = async (ctx, next) => { 
  var content = ctx.request.body;
  var url = URLConfig.url.user.login;  
  logger.info("用户登录："+url+",request:"+JSON.stringify(content));
  var responseData = await RequestUtil.GET(ctx,url,content); // 这里调用要在 async 函数里
  logger.info("用户登录response："+JSON.stringify(responseData));
  // ctx.response.body = responseData;  
  let user = responseData;
  if(user.success){
    const userInfo = {
      id: user.obj.id,
      loginname: user.obj.loginname,
      name: user.obj.name,
      mobile:user.obj.mobile
    }
    ctx.session.userInfo = userInfo;
  }else{
    ctx.response.body = {
      code: 200,
      msg: 'false'
    }
  }
}
 
exports.create = async (ctx, next) => {
  var url = URLConfig.url.user.create
	var content = ctx.request.body;
	logger.info("user.create req："+url+",请求JSON："+JSON.stringify(content));
	var responseData = await RequestUtil.POST(ctx,url,content); // 这里调用要在 async 函数里
	logger.info("user.create res："+JSON.stringify(responseData))
	ctx.response.body = responseData;  
}

exports.update = async (ctx, next) => {
  var url = URLConfig.url.user.update
	var content = ctx.request.body;
	logger.info("user.update req："+url+",请求JSON："+JSON.stringify(content));
	var responseData = await RequestUtil.POST(ctx,url,content); // 这里调用要在 async 函数里
	logger.info("user.update res："+JSON.stringify(responseData))
	ctx.response.body = responseData;  
}
exports.list = async (ctx, next) => {
  var url = URLConfig.url.user.list
	var content = ctx.request.body;
	logger.info("user.list req："+url+",请求JSON："+JSON.stringify(content));
	var responseData = await RequestUtil.POST(ctx,url,content); // 这里调用要在 async 函数里
	logger.info("user.list res："+JSON.stringify(responseData))
	ctx.response.body = responseData;  
}
exports.passwordReset = async (ctx, next) => {
  var url = URLConfig.url.user.passwordReset
	var content = ctx.request.body;
	logger.info("user.passwordReset  req："+url+",请求JSON："+JSON.stringify(content));
	var responseData = await RequestUtil.POST(ctx,url,content); // 这里调用要在 async 函数里
	logger.info("user.passwordReset res："+JSON.stringify(responseData))
	ctx.response.body = responseData;  
}

exports.roleCreate = async (ctx, next) => {
  var url = URLConfig.url.user.roleCreate
	var content = ctx.request.body;
	logger.info("user.roleCreate req："+url+",请求JSON："+JSON.stringify(content));
	var responseData = await RequestUtil.POST(ctx,url,content); // 这里调用要在 async 函数里
	logger.info("user.roleCreate res："+JSON.stringify(responseData))
	ctx.response.body = responseData;  
}

exports.roleUpdate = async (ctx, next) => {
  var url = URLConfig.url.user.roleUpdate
	var content = ctx.request.body;
	logger.info("user.roleUpdate req："+url+",请求JSON："+JSON.stringify(content));
	var responseData = await RequestUtil.POST(ctx,url,content); // 这里调用要在 async 函数里
	logger.info("user.roleUpdate res："+JSON.stringify(responseData))
	ctx.response.body = responseData;  
}

exports.roleList = async (ctx, next) => {
  var url = URLConfig.url.user.roleList
	var content = ctx.request.body;
	logger.info("user.roleList req："+url+",请求JSON："+JSON.stringify(content));
	var responseData = await RequestUtil.POST(ctx,url,content); // 这里调用要在 async 函数里
	logger.info("user.roleList res："+JSON.stringify(responseData))
	ctx.response.body = responseData;  
}
 
exports.resourceCreate = async (ctx, next) => {
  var url = URLConfig.url.user.resourceCreate
	var content = ctx.request.body;
	logger.info("user.resourceCreate req："+url+",请求JSON："+JSON.stringify(content));
	var responseData = await RequestUtil.POST(ctx,url,content); // 这里调用要在 async 函数里
	logger.info("user.resourceCreate res："+JSON.stringify(responseData))
	ctx.response.body = responseData;  
}

exports.resourceDelete = async (ctx, next) => {
  var url = URLConfig.url.user.resourceDelete
	var content = ctx.request.body;
	logger.info("user.resourceDelete req："+url+",请求JSON："+JSON.stringify(content));
	var responseData = await RequestUtil.POST(ctx,url,content); // 这里调用要在 async 函数里
	logger.info("user.resourceDelete res："+JSON.stringify(responseData))
	ctx.response.body = responseData;  
}

exports.resourceUpdate = async (ctx, next) => {
  var url = URLConfig.url.user.resourceUpdate
	var content = ctx.request.body;
	logger.info("user.resourceUpdate req："+url+",请求JSON："+JSON.stringify(content));
	var responseData = await RequestUtil.POST(ctx,url,content); // 这里调用要在 async 函数里
	logger.info("user.resourceUpdate res："+JSON.stringify(responseData))
	ctx.response.body = responseData;  
}

exports.resourceFind = async (ctx, next) => {
  var url = URLConfig.url.user.resourceFind
	var content = ctx.request.body;
	logger.info("user.resourceFind req："+url+",请求JSON："+JSON.stringify(content));
	var responseData = await RequestUtil.POST(ctx,url,content); // 这里调用要在 async 函数里
	logger.info("user.resourceFind res："+JSON.stringify(responseData))
	ctx.response.body = responseData;  
}

exports.resourceList = async (ctx, next) => {
  var url = URLConfig.url.user.resourceList
	var content = ctx.request.body;
	logger.info("user.resourceList req："+url+",请求JSON："+JSON.stringify(content));
	var responseData = await RequestUtil.POST(ctx,url,content); // 这里调用要在 async 函数里
	logger.info("user.resourceList res："+JSON.stringify(responseData))
	ctx.response.body = responseData;  
}

exports.resourceListMenu = async (ctx, next) => {
  var url = URLConfig.url.user.resourceListMenu
	var content = ctx.request.body;
	logger.info("user.resourceListMenu req："+url+",请求JSON："+JSON.stringify(content));
	var responseData = await RequestUtil.POST(ctx,url,content); // 这里调用要在 async 函数里
	logger.info("user.resourceListMenu res："+JSON.stringify(responseData))
	ctx.response.body = responseData;  
}